/*
 * DesktopDelegate.cpp  0.4.0 / Dec 04, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "DesktopDelegate.h"

#include "ComponentDelegate.h"
#include "SwingImageDelegate.h"

// DesktopDelegate :: Static Native Calls. -------------------------------------

/*!
 * \brief Returns the screen size.
 *
 * \param env the JNIEnv pointer
 * \param cls the Desktop java class
 * 
 * \return the screen size
 */
jobject DesktopDelegate::getScreenSize(JNIEnv *env, jclass cls)
{
  Q_UNUSED(cls);
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT_RETURN(DesktopDelegate, getScreenSize, jobject,
                                SWING_ENV_ARG, JCLASS_ARG(cls))
  QRect screenRect = QApplication::desktop()->screenGeometry();
  return DimensionObject::newObject(screenRect.width(), screenRect.height());
}

/*!
 * \brief Returns screen resolution.
 *
 * \param env the JNIEnv pointer
 * \param cls the Swing class
 *
 * \return screen resolution
 */
int DesktopDelegate::getScreenResolution(JNIEnv *env, jclass cls)
{
  Q_UNUSED(cls);
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT_RETURN(DesktopDelegate, getScreenResolution, int,
                                SWING_ENV_ARG, JCLASS_ARG(cls))
  return QApplication::desktop()->physicalDpiY();
}

/*!
 * \brief Returns desktop bounds.
 *
 * \param env the JNIEnv pointer
 * \param cls the java class
 * 
 * \return desktop bounds
 */
jobject DesktopDelegate::getDesktopBounds(JNIEnv *env, jclass cls)
{
  Q_UNUSED(cls);
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT_RETURN(DesktopDelegate, getDesktopBounds, jobject,
                                SWING_ENV_ARG, JCLASS_ARG(cls))
  QRect desktopRect = QApplication::desktop()->availableGeometry();
  return RectangleObject::newObject(desktopRect.x(), desktopRect.y(),
                                    desktopRect.width(), desktopRect.height());
}

/*!
 * \brief Returns screen shot of the specified component.
 *
 * \param env the JNIEnv pointer
 * \param cls the Desktop java class
 * \param componentDelegate the component delegate
 * \param imageFormat the image format
 * 
 * \return screen shot of the specified component
 */
jobject DesktopDelegate::takeScreenshot(JNIEnv *env, jclass cls,
                                        jlong componentDelegate,
                                        jstring imageFormat)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT_RETURN(DesktopDelegate, takeScreenshot, jobject,
                                SWING_ENV_ARG, JCLASS_ARG(cls),
                                JLONG_ARG(componentDelegate),
                                JSTRING_ARG(imageFormat))
  QWidget *widget;
  if (componentDelegate) {
    widget = COMPONENT_CAST(componentDelegate)->qWidget();
  } else {
    widget = QApplication::desktop();
  }
  return SwingImageDelegate::newInstance(QPixmap::grabWindow(widget->winId()),
                                         imageFormat);
}
